<?php

namespace App\Console\Commands\Fy;

use App\Imports\Fy\FyRegionImport;
use App\Models\FuYou\WorkOrderFuyouInfoModel;
use App\Models\Fy\FyRegionModel;
use App\Models\WorkOrder\WorkOrderApplyContentModel;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class GenerateCompanyName extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fy:generateCompanyName';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '富友商户信息-企业名称初始化';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        WorkOrderFuyouInfoModel::query()
            ->chunk(100, function ($item) use (&$batchData) {
                foreach ($item as $v) {
                    if ($v->companyName == '' && $v->workOrderNo) {
                        $content = WorkOrderApplyContentModel::query()
                            ->where('workOrderNo', $v->workOrderNo)->value('content');
                        $contentArray = json_decode($content, true);
                        if ($contentArray) {
                            $mchntName = $contentArray['enterpriseName'] ?? "";//企业名称
                            if ($mchntName && $v->id) {
                                WorkOrderFuyouInfoModel::setInfoById($v->id, ['companyName' => $mchntName]);
                            }
                        }
                    }
                }
            });
        return 1;
    }
}
